﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.DocCode"/>.
    /// </summary>
    public class DocCodeVM : DocCommentVM, IBlockVM
    {
        #region /* STATICS */

        internal static new void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(DocCode),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new DocCodeVM((DocCode)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.DocCode"/>.
        /// </summary>
        public DocCodeVM(DocCode docCode, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(docCode, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.DocCode"/> model.
        /// </summary>
        public DocCode DocCode
        {
            get { return (DocCode)CodeObject; }
        }

        /// <summary>
        /// The <see cref="BlockVM"/> body.
        /// </summary>
        public BlockVM BodyVM
        {
            get { return _contentVM as BlockVM; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderStart(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderStart(renderer, flags);

            // Force content to be on a new line if it's code objects
            if (_contentVM is CodeObjectVM && !((CodeObjectVM)_contentVM).IsFirstOnLine && !UseAlternativeFormat)
                RenderDocNewLines(renderer, 1, flags, this);
        }

        protected override void RenderContent(CodeRenderer renderer, TextStyle textStyle, RenderFlags flags)
        {
            bool indent = (_contentVM is CodeObjectVM);
            if (indent)
            {
                // Indent the content of the DocCode in lieu of the '///' prefix on each line
                renderer.IndentOnNewLineBegin(this);
            }
            if (_contentVM is string)
                textStyle &= ~TextStyle.Proportional;
            base.RenderContent(renderer, textStyle, flags);
            if (indent)
                renderer.IndentOnNewLineEnd(this);
        }

        protected override void RenderEnd(CodeRenderer renderer, RenderFlags flags)
        {
            // Force end tag to be on a new line if content is code objects
            if (_contentVM is CodeObjectVM && !UseAlternativeFormat)
                RenderDocNewLines(renderer, 1, flags, this);
            base.RenderEnd(renderer, flags);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            if (_contentVM is CodeObjectVM)
                ((CodeObjectVM)_contentVM).RenderVisible(renderer, flags);
        }

        public override void UnRender()
        {
            if (_contentVM is CodeObjectVM)
                ((CodeObjectVM)_contentVM).UnRender();
            base.UnRender();
        }

        #endregion
    }
}
